/**
 * Created by Alice on 2016/9/13 0013
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        judge: [],
        judge_name: [],
        courtrooms: [],
        courtrooms_name: [],
        page_size: 12
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量

        var currentDay = utils.get_date(0);
        //查询的开始时间默认给今天
        $('#startTime').datetimepicker({
            bootcssVer: 3,
            minView: "month", //选择日期后，不会再跳转去选择时分秒
            weekStart: "1",
            format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            autoclose: true //选择日期后自动关闭
        }).on('changeDate', function (ev) {
            console.log(ev.date.valueOf());
        });
        $('#endTime').datetimepicker({
            bootcssVer: 3,
            minView: "month", //选择日期后，不会再跳转去选择时分秒
            weekStart: "1",
            format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            autoclose: true //选择日期后自动关闭
        }).on('changeDate', function (ev) {
            console.log(ev.date.valueOf());
        });

        init_event();
        function init_event() {//TODO 初始化页面事件
            $("#startTime").find("input").val(currentDay);
            $("#endTime").find("input").val(currentDay);

            Class.select_judge_group();
            // Class.init_push();

            $("#conditionBtn").click(function () {
                $("#loading").show();
                Class.count_case();
                $("#loading").hide();
            });
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
            var judge_id = utils.get_url_param("judge");
            judge_id ? ready.judge.push(judge_id) : '';
            var courtroom_id = utils.get_url_param("courtroom_id");
            courtroom_id ? ready.courtrooms.push(courtroom_id) : '';
        }
    };
    //TODO 内部调用的方法写这里
    var Class = {

        select_judge_group: function () {
            var dropmenu_judge = $('#allJudges').empty();
            if (groupUser && groupUser.length > 0) {
                dropmenu_judge.append('<option value="" selected>所有法官</option>');


                if (ready.judge.length > 0) {
                    var exist = "not";
                    $.each(groupUser, function (index, group) {
                        if (group.userUid == ready.judge[0]) {
                            dropmenu_judge.empty();
                            dropmenu_judge.append('<option value="' + group.userUid + '" selected>' + group.userName + '</option>');
                            $(".active").find("span").append("&nbsp;" + group.userName + "审理过的案件");//今日开庭和排期
                            ready.judge_name.push(group.userName);
                            exist = "yes";
                        }
                    });
                    //做一个不存在验证
                    if ("not" == exist) {
                        layui.use('layer', function () {
                            layui.layer.msg("此法官不存在或不属于本庭，展示全部!");
                        });
                        $.each(groupUser, function (index, group) {
                            dropmenu_judge.append('<option value="' + group.userUid + '">' + group.userName + '</option>');
                            ready.judge.push(group.userUid);
                            ready.judge_name.push(group.userName);
                        });
                    }
                } else {
                    // console.log(group);
                    $.each(groupUser, function (index, group) {
                        dropmenu_judge.append('<option value="' + group.userUid + '">' + group.userName + '</option>');
                        ready.judge.push(group.userUid);
                        ready.judge_name.push(group.userName);
                    });
                }
            } else {
                layui.use('layer', function () {
                    layui.layer.msg("查询法官失败!");
                });
            }
            var dropmenu_courtroom = $('#allCourtRooms').empty();
            dropmenu_courtroom.append('<option value="" selected>所有法庭</option>');
            if (courtroomList && courtroomList.length > 0) {
                $.each(courtroomList, function (index, courtroom) {
                    dropmenu_courtroom.append('<option value="' + courtroom.uid + '">' + courtroom.name + '</option>');
                    ready.courtrooms.push(courtroom.uid);
                    ready.courtrooms_name.push(courtroom.name);
                })
            } else {
                layui.use('layer', function () {
                    layui.layer.msg("查询法庭失败!");
                });
            }
            Class.select_secretary();
            Class.select_case_type();
            Class.count_case();
        },
        select_case_type: function () {
            var case_type = $("#caseTypeName").empty();
            case_type.append("<option value=''>" + "所有类型" + "</option>");
            $.each(caseTypeList, function (i, valueOfElement) {
                case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
            });
        },
        select_secretary: function () {
            var select_secretary = $("#allSecretary").empty();
            if (ready.secretary) {
                var select_secretary = $("#allSecretary").empty();
                var exist1 = "not";
                $.each(secretaryList, function (i, secretary) {
                    if (secretary.uid == ready.secretary) {
                        select_secretary.append("<option value='" + secretary.uid + "' selected>" + secretary.name + "</option>");
                        select_secretary.find("input").val(secretary.uid);
                        $(".active").find("span").append("&nbsp;" + secretary.name + "审理过的案件");//今日开庭和排期
                        exist1 = "yes";
                    }
                });
                if ("not" == exist1) {
                    layui.use('layer', function () {
                        layui.layer.msg("未找到此书记员，展示全部!");
                    });
                    select_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                    $.each(secretaryList, function (i, valueOfElement) {
                        select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                }
            } else {
                select_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                $.each(secretaryList, function (i, valueOfElement) {
                    select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                });
            }
        },
        count_case: function (pageIndex) {
            if (!pageIndex) {
                pageIndex = 1;
            }
            var archives = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var judge = $("#allJudges").val() || ready.judge;
            var secretary = $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            // console.log(caseName, caseTypeName, courtRoom, judge, secretary, startTime, endTime);
            var row_no_padding = $(".row-no-padding").empty();
            $.ajax({
                url: r.base_path + "judge/selectMoreCasePlanOrReview",
                type: "get",
                dataType: "json",
                data: {
                    courtUid: r.user_court_uid,
                    archives: archives,
                    caseName: caseName,
                    type: caseTypeName,
                    courtRoom: courtRoom,
                    judgeList: judge.toString(),
                    secretaryId: secretary,
                    startDate: startTime,
                    endDate: endTime,
                    status: 0,
                    review: 0,
                    page: pageIndex,
                    pageSize: ready.page_size
                },
                success: function (result) {
                    // console.log(result.data);
                    var s = result.data.count;
                    if (s == 0) {
                        Class.page(1, 1, null);
                    } else {
                        Class.page(pageIndex, Math.ceil(s / 12), Class.count_case);
                    }

                    if (result.code == 0) {
                        if (result.data.count == 0) {
                            row_no_padding.append('<span>暂无开庭信息</span>');
                        } else {
                            var picNum = 6;
                            $.each(result.data.data, function (i, valueOfElement) {
                                var caseName = valueOfElement.name;
                                var startDate = valueOfElement.startDate;
                                var planUid = valueOfElement.planUid;
                                var status = valueOfElement.status;
                                var courtroom_name = valueOfElement.courtroomName;
                                var courtroomUid = valueOfElement.courtroomUid;
                                var statusName = "";
                                if (0 == status) {
                                    statusName = "排期";
                                } else if (1 == status || 5 == status) {
                                    statusName = "开庭";
                                } else if (2 == status) {
                                    statusName = "休庭";
                                } else if (3 == status) {
                                    statusName = "闭庭";
                                } else {
                                    statusName = "归档";
                                }
                                var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                                var href = r.base_path + "group/living?planUid=" + planUid + "&courtroomUid=" + courtroomUid;
                                var html = '<div class="col-md-2 col-xs-6">' +
                                    '<div class="more-courting-item">' +
                                    '<a href="' + href + '">' +
                                    '<div class="more-courting-item-img-panel">' +
                                    '<p class="more-courting-item-img-panel-top">' + (courtroom_name ? courtroom_name : '未知') + '</p>' +
                                    '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                                    '<p class="more-courting-item-img-panel-bottom" >' + statusName + '</p>' +
                                    '</div>' +
                                    '<p id="startDate">' + startDate + '</p>' +
                                    '<p id="caseNoDis">' + (valueOfElement.archives ? valueOfElement.archives : '未知') + '</p>' +
                                    '<p class="more-courting-item-depiction">' +
                                    // '<i class="fa fa-exchange cursor-hand" title="推送" id="' + valueOfElement.uid + '"></i>' +
                                    '<span>' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                    '</p>' +
                                    '</a>' +
                                    '</div>' +
                                    '</div>';
                                row_no_padding.append(html);
                            });
                            $('.fa-exchange').click(function (e) {
                                e.preventDefault();
                                // console.log();
                                $('#_uid').val(this.id);
                                $("#_detail").modal("show");
                            })
                        }
                        $("#loading").hide();
                        row_no_padding.show();
                    }
                }
            }).fail(function () {
                $('#loading').hide();
            });
        },
        init_push: function () {
            var court_push = $("#_courtPush").empty();
            $(r.court_list).each(function (i, n) {
                court_push.append("<option value='" + n.courtid + "'>" + n.courtname + "</option>");
            });
            Class.select_courtroom(r.court_list[0].courtid);
            court_push.change(function () {
                var val = $(this).children('option:selected').val();
                Class.select_courtroom(val);
            })
        },
        select_courtroom: function (id) {
            $.post(r.base_path + "sys/courtEquipment/selectCourtRoom", {courtId: id}, function (result) {
                if (result.data) {
                    $("#_courtRoom").empty();
                    $.each(result.data, function (i, n) {
                        $("#_courtRoom").append("<option value='" + n.uid + "'>" + n.courtroomname + "</option>");
                    });
                }
            }, 'json')
        },
        save: function () {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.confirm('想要推送该案件吗?<br>将要推送到:<br>"' +
                    $('#_courtPush').find("option:selected").text() + '<br>' + $('#_courtRoom').find("option:selected").text() + '"', {
                    btn: ['是的', '取消'] //按钮
                }, function () {
                    $.post(r.base_path + "planTransfer/planTransferDown", {
                        planid: $('#_uid').val(),
                        downcourtid: $('#_courtPush').val(),
                        courtroomid: $('#_courtRoom').val()
                    }, function (result) {
                        if (result && result.status == 0) {
                            layer.msg('推送成功！');
                        } else {
                            layer.msg(result.msg);
                        }
                        $("#_detail").modal("hide");
                    }, 'json').fail(function () {
                        $("#_detail").modal("hide");
                    })
                }, function () {

                });
            });
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }

    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {
        save: Class.save
    };
}(window);